package com.car.servlet;

import com.car.service.UserService;
import com.car.entyti.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    UserService userService= new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");



        String acc=req.getParameter("account");
        String pwd=req.getParameter("password");
        String name=req.getParameter("userName");
        String phone = req.getParameter("phone");


        User user=new User();
        user.setAccount(acc);
        user.setPassword(pwd);
        user.setName(name);
        user.setPhone(phone);


        if (userService.register(user)!= null){
            req.getSession().setAttribute("msg","注册成功");
            resp.sendRedirect(req.getContextPath()+"/login.jsp");

        }else {
            req.getSession().setAttribute("msg","注册失败");
            resp.sendRedirect(req.getContextPath()+"/register.jsp");
        }
    }
}
